package com.yxl.toolsite.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.time.temporal.TemporalField;

@RestController
@RequestMapping("/rest/time")
public class TimestampController {

    static  DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @GetMapping("/time2ms")
    public long time2ms(@RequestParam("dateStr") String dateStr) {
        LocalDateTime dt = LocalDateTime.parse(dateStr,formatter);
        return dt.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    @GetMapping("/ms2time")
    public String ms2time(@RequestParam("timeInMil") long timeInMil) {
        Instant inst = Instant.ofEpochMilli(timeInMil);
        return LocalDateTime.ofInstant(inst,ZoneOffset.of("+8")).format(formatter);
    }
}
